package cn.edu.ctbu.datajpa01.demos.web;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {

    @Autowired
    private StudentRepository studentRepository; // 注入学生仓库接口

    @PostMapping("/model")
    public String getFirstStudent(Model model) {
        // 获取第一个学生数据
        Student firstStudent = (Student) studentRepository.findAll();

        // 将学生数据传递到Thymeleaf模板中
        model.addAttribute("student", firstStudent);

        // 返回Thymeleaf模板文件名
        return "src/main/resources/static/model.html";
    }
}
